package com.example.myrecipes;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class RecipeBoxActivity extends ListActivity {
	private static final int CREATE_RECIPE = 0;
	private static final int VIEW_RECIPE = 1;
	ArrayAdapter<Recipe> arrayAdapt;
	ArrayList<Recipe> recipes;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Show the Up button in the action bar.
		setupCustomOptionMenu();

		Intent intent = getIntent();
		handleIntent(intent);

		/*RecipeBox recipeBox = intent.getExtras().getParcelable("RecipeBox");
		ArrayList<Recipe> recipes = recipeBox.getRecipeList();

		String recipeBoxName = recipeBox.getRecipeBoxName();
		//TextView textView = (TextView) findViewById(R.id.recipe_box_name);
		//textView.setText(recipeBoxName);

		if(recipes == null) {
			recipes = new ArrayList<Recipe>();
		}

		arrayAdapt = new ArrayAdapter<Recipe>(this, android.R.layout.simple_list_item_1,recipes);
		setListAdapter(arrayAdapt);
		setContentView(R.layout.activity_recipe_box);*/
	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	private void setupCustomOptionMenu() {
		LayoutInflater inflater = (LayoutInflater) getActionBar().getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
		final View customActionBarView = inflater.inflate(R.layout.view_recipebox_menu, null);
		customActionBarView.findViewById(R.id.actionbar_back).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		customActionBarView.findViewById(R.id.actionbar_create).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						createRecipe(v);
					}
				});
		setupActionBar(customActionBarView);
	}

	private void setupActionBar(View customActionBarView) {
		final ActionBar actionBar = getActionBar();
		actionBar.setDisplayOptions(
				ActionBar.DISPLAY_SHOW_CUSTOM,
				ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
				| ActionBar.DISPLAY_SHOW_TITLE);
		actionBar.setCustomView(customActionBarView, new ActionBar.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
	}

	public void handleIntent(Intent intent) {
		if(intent.getExtras() != null) {
			RecipeBox recipeBox = intent.getExtras().getParcelable("RecipeBox");
			recipes = recipeBox.getRecipeList();

			if(recipes == null) {
				recipes = new ArrayList<Recipe>();
			}
			arrayAdapt = new ArrayAdapter<Recipe>(this, android.R.layout.simple_list_item_1,recipes);
			setListAdapter(arrayAdapt);
			
			setContentView(R.layout.activity_recipe_box);
		}
	}

	public void onListItemClick(ListView l, View v, int position, long id) {
		Recipe selection = (Recipe) l.getItemAtPosition(position);
		Intent intent = new Intent(getApplicationContext(), RecipeActivity.class);
		intent.putExtra("Recipe", (Parcelable) selection);
		startActivityForResult(intent, VIEW_RECIPE);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.context_menu, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.edit: {
			return true;
		}
		case R.id.delete: {
			Toast.makeText(this, (int) info.id, Toast.LENGTH_LONG).show();
			return true;
		}
		default:
			return super.onContextItemSelected(item);
		}
	}
	
	public void createRecipe(View view) {

		Intent intent = new Intent(getApplicationContext(), EditRecipe.class);

		startActivityForResult(intent, CREATE_RECIPE);

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch(requestCode) {
		case (CREATE_RECIPE) : {
			if (resultCode == Activity.RESULT_OK) {
				Recipe recipe = data.getExtras().getParcelable("Recipe");
				arrayAdapt.add(recipe);
			}
			break;
		}
		case(VIEW_RECIPE) : {
			break;
		}
		}
	}
}
